package com.pn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pn.entity.Store;
import com.pn.mapper.StoreMapper;
import com.pn.service.StoreService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.StoreServiceImpl")
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;

    //显示仓库列表
    @Override
    public PageBean storePageList(Store store) {
        PageHelper.startPage(store.getPageNum(),store.getPageSize());
        Page page = storeMapper.storePageList(store);
        return new PageBean(page.getTotal(),page.getResult());
    }

    //修改仓库
    @CacheEvict(key = "'all:store'")
    @Override
    public Result storeUpdate(Store store) {
        int i = storeMapper.storeUpdate(store);
        if (i > 0 ){
            return Result.ok("修改仓库成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"修改仓库失败");
    }

    @Cacheable(key = "'all:store'")
    @Override
    public List<Store> storeList() {
        List<Store> storeList = storeMapper.storeList();
        return storeList;
    }


    //查看仓库编号是否存在
    @Override
    public Result storeNumCheck(String storeNum1) {
        List<Store> store = storeMapper.storeNumCheck(storeNum1);
        if (store.size() != 0){
            return Result.err(Result.CODE_ERR_BUSINESS,"仓库编号已存在");
        }

        return Result.ok(true);
    }


    //添加仓库
    @CacheEvict(key = "'all:store'")
    @Override
    public Result storeAdd(Store store) {
        int i = storeMapper.sotreInsert(store);
        if (i > 0 ){
            return Result.ok("添加仓库成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"添加仓库失败");
    }

    //删除仓库
    @CacheEvict(key = "'all:store'")
    @Override
    public Result storeDelete(int storeId) {
        int i = storeMapper.storeDelete(storeId);
        if (i > 0 ){
            return Result.ok("删除仓库成功");
        }

        return Result.err(Result.CODE_ERR_BUSINESS,"删除仓库失败");
    }



}
